package com.tjakus.properties;

import java.io.FileInputStream;
import java.util.Properties;
import com.tjakus.logging.Logging;

/**
 * This is a static class which represents all settings/properties which will be
 * used.
 * 
 * @author tjakus
 * 
 */

public class Property {
	public static String USER_DATA_CRON_SCHEDULE;
	public static String RENEW_ADV_CRON_SCHEDULE;
	public static String IS_PROXY_ENABLED;
	public static String PROXY_HOST;
	public static int PROXY_PORT;

	/**
	 * This method retrieves all settings/properties from database and initializes
	 * them. It returns true or false.
	 * 
	 */
	public static boolean initializeProperties() {
		Properties properties = new Properties();
		try {
			Logging.getLogger().info(
					"Application - main - [ reading property file ] ");
			// Read configuration from property file
			properties.load(new FileInputStream("config.properties"));
			// get the property value and print it out
			USER_DATA_CRON_SCHEDULE = properties.getProperty("USER_DATA_CRON_SCHEDULE");
			Logging.getLogger().debug(
					"Application - main - [ Cron schedule: " + USER_DATA_CRON_SCHEDULE + " ] ");
			RENEW_ADV_CRON_SCHEDULE = properties.getProperty("RENEW_ADV_CRON_SCHEDULE");
			Logging.getLogger().debug(
					"Application - main - [ Cron schedule: " + RENEW_ADV_CRON_SCHEDULE + " ] ");
			IS_PROXY_ENABLED = properties.getProperty("IS_PROXY_ENABLED");
			Logging.getLogger().debug(
					"Application - main - [ Is proxy enabled: " + IS_PROXY_ENABLED + " ] ");
			// If proxy is enabled then load proxy properties
			if (Property.IS_PROXY_ENABLED.equalsIgnoreCase("TRUE")) {
				PROXY_HOST = properties.getProperty("PROXY_HOST");
				Logging.getLogger().debug(
						"Application - main - [ Proxy host: " + PROXY_HOST + " ] ");
				PROXY_PORT = Integer.parseInt(properties.getProperty("PROXY_PORT"));
				Logging.getLogger().debug(
						"Application - main - [ Proxy port: " + PROXY_PORT + " ] ");
			}

		} catch (Exception ex) {
			Logging.getLogger().error(
					"Application - main - [ Problem reading property file ] ");
			Logging.getLogger().error(ex);
			return false;
		}
		return true;
	}

}
